package com.lw.extraceim.ui.fragments.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;


import com.lw.extraceim.app.ExtraceApplication;
import com.lw.extraceim.ui.activity.base.BaseActivity;

public abstract class BaseFragment extends Fragment {
    protected Context mContext;
    protected ExtraceApplication application;//全局Application

    public BaseFragment(Context mContext, ExtraceApplication application) {
        this.mContext = mContext;
        this.application = application;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return  initView();
    }
    //抽象类由子类实现
    public abstract View initView();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initEvent();
        refreshUI();
    }
    //当子类需要联网请求数据的时候，可以重写该方法，在该方法联网
    public abstract void initData();

    public abstract void initEvent();

    public abstract void refreshUI();

    //显示提示框
    public void showDialog(String title, String message, String positiveButtonStr,
                           String negativeButtonstr,
                           DialogInterface.OnClickListener positiveListener,
                           DialogInterface.OnClickListener negativeListener) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setPositiveButton(positiveButtonStr, positiveListener);
        dialog.setNegativeButton(negativeButtonstr, negativeListener);
        dialog.show();
    }

    /**
     * 显示提示框
     *
     * @param message
     * @return
     */
    public void alert(String message) {
        showDialog("提示", message, "确定", null, null, null);
    }

    /**
     * 显示 Toast
     *
     * @param message
     */
    public void toast(String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
        refreshUI();
    }
}
